package com.antis.olsl.data.introduction;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewOutlineProvider;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.antis.olsl.arouterlib.DataRoutePath;
import com.antis.olsl.base.BaseActivity;
import com.antis.olsl.data.adapter.IntroductionGoodsListAdapter;
import com.antis.olsl.data.databinding.ActivityIntroductionSlipDetailsBinding;
import com.antis.olsl.library.bean.GoodsInfo;
import com.antis.olsl.library.bean.IntroductionInfo;
import com.antis.olsl.library.common.ConstantsPool;
import com.antis.olsl.library.utils.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @ProjectName: ATS_OLSL
 * @Package: com.antis.olsl.activity.purchaseQuery
 * @ClassName: PurchaseSlipDetailsActivity
 * @Description: 盘点详情
 * @Author: 羊琪
 * @CreateDate: 2020/3/30 19:46
 * @UpdateDate: 2020/3/30 19:46
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Route(path = DataRoutePath.ACTIVITY_INTRODUCTION_SLIP_DETAIL)
public class IntroductionSlipDetailsActivity extends BaseActivity<ActivityIntroductionSlipDetailsBinding> implements OnLoadMoreListener {

    int pageNum = 1;
    int maxPage = 1;
    @Autowired
    boolean type;
    @Autowired
    String purchaseSlipId;
    private ArrayList<GoodsInfo> goodsInfos = new ArrayList<>();
    private IntroductionGoodsListAdapter adapter;

    @Override
    protected ActivityIntroductionSlipDetailsBinding getViewBinding() {
        return ActivityIntroductionSlipDetailsBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    protected void initViews() {
        ARouter.getInstance().inject(this);
        setSupportActionBar(viewBinding.toolbar);
        //去掉上滑时顶部阴影
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            viewBinding.appBarLayout.setOutlineProvider(null);
            viewBinding.coordinatorLayout.setOutlineProvider(ViewOutlineProvider.BOUNDS);
        }

        adapter = new IntroductionGoodsListAdapter(goodsInfos);
        viewBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        viewBinding.recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putBoolean(ConstantsPool.FROM_SALESROOM_OR_WAREHOUSE, type);
                bundle.putString(ConstantsPool.PURCHASE_SLIP_ID, purchaseSlipId);
                bundle.putString(ConstantsPool.GOODS_ID, goodsInfos.get(position).getProductId());
                // readyGo(IntroductionSlipGoodsDetailsActivity.class, bundle);
            }
        });
        adapter.getLoadMoreModule().setOnLoadMoreListener(this);
    }

    @Override
    protected void initData() throws IllegalAccessException {
        viewBinding.tvId.setText(purchaseSlipId);

        getDetails();
        getGoodsList();
    }

    /**
     * 获取调拨单详情
     */
    private void getDetails() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("introductionNumber", purchaseSlipId);
        // okHttpClient.postJsonObjectParams(fromSalesroom ? UrlServerConnections.GET_INTRODUCTION_SLIP_DETAILS : UrlServerConnections.GET_INTRODUCTION_SLIP_DETAILS, map, GetIntroductionDetailsRes.class);
    }

    /**
     * 获取商品列表
     */
    private void getGoodsList() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("introductionNumber", purchaseSlipId);
        map.put("pageNum", pageNum);
        map.put("pageSize", 10);
        // okHttpClient.postJsonObjectParams(fromSalesroom ? UrlServerConnections.GET_GOODS_LIST_FOR_INTRODUCTION : UrlServerConnections.GET_GOODS_LIST_FOR_INTRODUCTION, map, GetIntroductionGoodsListRes.class);
    }

//    public void successResponse(BaseRes baseRes) {
//        if (baseRes instanceof GetIntroductionDetailsRes) {
//            GetIntroductionDetailsRes slipDetailsRes = (GetIntroductionDetailsRes) baseRes;
//            if (slipDetailsRes.getContent() != null) {
//                flushData(slipDetailsRes.getContent());
//            }
//        } else if (baseRes instanceof GetIntroductionGoodsListRes) {
//            GetIntroductionGoodsListRes goodsListRes = (GetIntroductionGoodsListRes) baseRes;
//            if (pageNum == 1) {
//                goodsInfos.clear();
//            }
//            if (goodsListRes.getContent() != null) {
//                maxPage = goodsListRes.getContent().getTotal() % 10 == 0 ? goodsListRes.getContent().getTotal() / 10 : goodsListRes.getContent().getTotal() / 10 + 1;
//                if (goodsListRes.getContent().getObject() != null) {
//                    goodsInfos.addAll(goodsListRes.getContent().getObject());
//                }
//
//                adapter.setList(goodsInfos);
//                adapter.getLoadMoreModule().setEnableLoadMore(pageNum < maxPage);
//                adapter.getLoadMoreModule().loadMoreComplete();
//            }
//        }
//    }

    private void flushData(IntroductionInfo slipInfo) {
        if (slipInfo == null) {
            return;
        }

        viewBinding.tvStatus.setText(slipInfo.getAuditStatusText());
        viewBinding.tvStatus.setSelected(slipInfo.getAuditStatus() == 2);
        viewBinding.tvDate.setText(slipInfo.getCreateTime());
        viewBinding.tvIntroudctionPerson.setText(slipInfo.getIntroductionPerson());
        viewBinding.tvReason.setText(slipInfo.getReason());
        viewBinding.tvSkuNum.setText(StringUtils.getCommaFormat(BigDecimal.valueOf((double) slipInfo.getSkuNum())));
    }

    @Override
    public void onLoadMore() {
        if (pageNum == 1) {
            adapter.getLoadMoreModule().setEnableLoadMore(false);
        }
        if (pageNum < maxPage) {
            pageNum++;
            getGoodsList();

        } else {
            adapter.getLoadMoreModule().loadMoreEnd(false);
        }
    }
}
